//@author A0097846J
package tasky.ui;

import java.awt.event.KeyEvent;

import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;

/**
 * Using the JNativeHook library. 
 * This Class controls what keys to listen to globally, and execute
 * commands from the BaseWindow class depending on the keys pressed. Global
 * listening means that the key listeners will still trigger even when Tasky
 * is off-focused on screen
 *
 */

public class GlobalKeyListener implements NativeKeyListener {

	private BaseWindow baseFrame;
	private static final int PROG_OPEN = 0;

	public GlobalKeyListener(BaseWindow mainFrame) {
		baseFrame = mainFrame;
	}

	public void nativeKeyPressed(NativeKeyEvent e) {

		if (userPressedF9(e)) {
			minimaxiProg();
		} else if (userPressedPgUp(e)) {
			baseFrame.moveDisplayUp();
		} else if (userPressedPageDown(e)) {
			baseFrame.moveDisplayDown();
		} else if (userPressedF1(e)) {
			baseFrame.popoutHelp();
		}
		
	}

	private boolean userPressedF(NativeKeyEvent e) {
		return e.getKeyCode() == KeyEvent.VK_F;
	}

	private void minimaxiProg() {
		if (frameIsOpen()) {
			baseFrame.miniProgram();
		} else {
			baseFrame.maxiProgram();
		}
	}

	private boolean frameIsOpen() {
		return baseFrame.getStateOfProgram() == PROG_OPEN;
	}

	private boolean userPressedF1(NativeKeyEvent e) {
		return e.getKeyCode() == KeyEvent.VK_F1
				&& frameIsOpen();
	}

	private boolean userPressedPageDown(NativeKeyEvent e) {
		return e.getKeyCode() == KeyEvent.VK_PAGE_DOWN
				&& frameIsOpen();
	}

	private boolean userPressedPgUp(NativeKeyEvent e) {
		return e.getKeyCode() == KeyEvent.VK_PAGE_UP
				&& frameIsOpen();
	}

	private boolean userPressedF9(NativeKeyEvent e) {
		return e.getKeyCode() == KeyEvent.VK_F9;
	}

	public void nativeKeyReleased(NativeKeyEvent e) {
		if (userPressedF(e) && frameIsOpen()) {
			regainFocus();
		}
	}

	private void regainFocus() {
		baseFrame.setFocusToUserInput();
	}
	

	public void nativeKeyTyped(NativeKeyEvent e) {
		
	}

}
